package com.czk.classreader.counttime;

import com.czk.sample.Test;

import java.lang.reflect.Field;
import java.util.Random;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/23 16:42
 */
public class Client {
    public static void main(String[] args) throws Exception {
        // 反射
        Class<?> clazz = Class.forName("com.czk.sample.Test");
        Test instance= (Test) clazz.newInstance();
        Random rand = new Random(System.currentTimeMillis());
        for (int i = 0; i < 10; i++) {
            boolean flag = rand.nextBoolean();
            int a = rand.nextInt(50);
            int b = rand.nextInt(50);
            if (flag) {
                int c = instance.addNum(a, b);
                String line = String.format("%d + %d = %d", a, b, c);
                System.out.println(line);
            }
            else {
                int c = instance.sub(a, b);
                String line = String.format("%d - %d = %d", a, b, c);
                System.out.println(line);
            }
        }

        // 拿到定义的成员变量值
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            String name = declaredField.getName();
            declaredField.setAccessible(true);
            if (name.startsWith("timer")) {
                Object FieldValue = declaredField.get(null);
                System.out.println(name + " = " + FieldValue);
            }
        }
    }
}
